package com.example.pceb.response;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.jsonwebtoken.lang.Collections;

import java.util.List;

public class PageResponse<T> {
    private int currentPage;    // 当前页码
    private int pageSize;       // 每页大小
    private long totalItems;    // 总记录数
    private int totalPages;     // 总页数
    private List<T> items;      // 数据列表

    /**
     * 从MyBatis-Plus分页对象创建
     * @param source MyBatis-Plus分页对象
     * @param items  当前页的数据项
     */
    public PageResponse(IPage<?> source, List<T> items) {
        this.currentPage = (int) source.getCurrent();
        this.pageSize = (int) source.getSize();
        this.totalItems = source.getTotal();
        this.totalPages = (int) source.getPages();
        this.items = items;
    }

    public PageResponse() {

    }

    /**
     * 创建空页
     */
    public static <T> PageResponse<T> empty() {
        PageResponse<T> response = new PageResponse<>();
        response.currentPage = 1;
        response.pageSize = 10;
        response.totalItems = 0;
        response.totalPages = 0;
        response.items = Collections.emptyList();
        return response;
    }

    // Getters 和 Setters
    public int getCurrentPage() {
        return currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public long getTotalItems() {
        return totalItems;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public List<T> getItems() {
        return items;
    }
}